package com.tb.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.tb.pojo.College;
import com.tb.pojo.Teacher;
import com.tb.pojo.Title;
import com.tb.service.CollegeService;
import com.tb.service.TeacherService;
import com.tb.service.TitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/tea")
public class TeacherController {

    @Autowired
    private TeacherService ts;

    @Autowired
    private CollegeService collegeService;

    @Autowired
    private TitleService titleService;


    /**
     * 获取教师的全部信息
     * @param mav 返回的视图模型对象
     * @param currentPage 当前的页数
     * @return
     */
    @RequestMapping("/getList")
    public ModelAndView getList(ModelAndView mav,
                                @RequestParam(value = "pg", defaultValue = "1") int currentPage) {
//        System.out.println("currentPage" + currentPage);
        PageInfo<Teacher> page = ts.findAll(currentPage);
        List<Title> titleList = titleService.findAll();
        List<College> collegeList = collegeService.findAll();
        mav.addObject("titleList", titleList);
        mav.addObject("collegeList", collegeList);
        mav.addObject("teacherList", page.getList());
        mav.addObject("currentPage", page.getPageNum());
        mav.addObject("pages", page.getPages());
        mav.addObject("page", page);
        mav.addObject("info", "欢迎来到教师系统");
        mav.setViewName("/teacher/information");
        return mav;
    }


    /**
     * 去离职的界面
     * @return
     */
    @RequestMapping("/toResign")
    public String toResign() {

        return "/teacher/resign";
    }

    /**
     * 教师离职
     * @param mav 返回的视图模型对象
     * @param tea 传入需要离职的教师信息
     * @return
     */
    @PostMapping("/resign")
    public ModelAndView Resign(ModelAndView mav,
                               Teacher tea) {
//        System.out.println(tea);
        boolean flag = ts.resign(tea);
        if (!flag) {
            mav.addObject("info", "查无此人,您的信息输入有误");
            mav.setViewName("/teacher/resign");
            return mav;
        }

        mav.setViewName("redirect:/tea/getList");
        return mav;
    }


    /**
     * 查看功能
     * @param tid 传入对应需要查看信息的教师的工号
     * @param model 将查询的数据发送到jsp
     * @return
     */
    @RequestMapping("/view")
    public String view(@RequestParam(value = "tid") String tid,
                       Model model) {
//        System.out.println(tid);
        Teacher teacher = ts.findOne(tid);
        model.addAttribute("teacher", teacher);
        return "/teacher/view";
    }

    /**
     * 去修改的页面
     * @return
     */
    @GetMapping("/toModify")
    public ModelAndView toModify(String tid,
                           ModelAndView mav) {
//        System.out.println(tid);
        Teacher teacher = ts.findOne(tid);
        mav.addObject("teacher", teacher);
        List<Title> titleList = titleService.findAll();
        List<College> collegeList = collegeService.findAll();
        mav.addObject("titleList", titleList);
        mav.addObject("collegeList", collegeList);
        mav.setViewName("/teacher/modify");
        return mav;
    }


    /**
     * 修改功能
     * @param teacher 传入的需要修改的教师信息
     * @return
     */
    @PostMapping("/modify")
    @ResponseBody
    public String modify(Teacher teacher) {
//        System.out.println(teacher);

        ts.modify(teacher);
        return "good";
    }


    /**
     * 去教师入职页面
     * @return
     */
    @GetMapping("/toRegister")
    public String toRegister() {

        return "/teacher/register";
    }


    /**
     * 入职功能
     * @param mav 视图模型对象
     * @param teacher 需要入职的教师信息
     * @return
     */
    @PostMapping("/register")
    public ModelAndView register(ModelAndView mav,
                           Teacher teacher) {
//        System.out.println(teacher);
        boolean flag = ts.addTea(teacher);
        if (flag) {
            mav.addObject("info", "注册成功");
            mav.setViewName("redirect:/tea/getList");
            return mav;
        }
        mav.addObject("info", "注册失败");
        mav.setViewName("redirect:/tea/toRegister");
        return mav;
    }


    @GetMapping("/condition")
    public ModelAndView condition(ModelAndView mav,
                                  int college,
                                  int title,
                                  @RequestParam(value = "pg", defaultValue = "1") int currentPage) {
//        System.out.println("1");
        PageInfo<Teacher> page= ts.findAllCondition(college, title, currentPage);
//        System.out.println("page1:" + page);
        if (page != null) {
            List<College> collegeList = collegeService.findAll();
            List<Title> titleList = titleService.findAll();
            mav.addObject("collegeList", collegeList);
            mav.addObject("titleList", titleList);
            mav.addObject("teacherList", page.getList());
            mav.addObject("currentPage", page.getPageNum());
            mav.addObject("pages", page.getPages());
            mav.addObject("page", page);
            mav.addObject("info", "查询成功");
        } else {
            mav.addObject("info", "您条件太苛刻了");
        }
        mav.setViewName("/teacher/information");
        return mav;

    }


}
